<?php


namespace api\modules\app\controllers;


use api\components\GeoService;
use api\modules\app\models\QrcodeBind;
use api\modules\app\models\Unit;
use api\modules\app\services\LogService;
use api\modules\app\services\UnitService;
use yii\debug\models\search\Log;
use yii\log\Logger;
use yii\rest\ActiveController;
use yii\web\Controller;

class TestController extends Controller
{

    public function actionT109() {
       $service = new LogService();
       $post['module_name'] = '部件';
       $post['user_id'] = 1;
       $post['username'] = 'acs';
       $post['sub_type'] = 2;
       $post['remark'] = 'test';
       $result = $service->logToUserCenter($post);

       return $result;

       exit;

        return 999;
//        $result = parent::createAction();
//       return $result;
//        print_r(\Yii::$app->get('test_3'));
//        echo 11111;exit;
//        parent::createAction();
//        echo 123; exit;
    }

    public function actionT1() {

//        if (extension_loaded('gd') && function_exists('gd_info')) {
//            echo '111';
//        } else {
//            echo 222; exit;
//        }

        $image = imagecreate(200,200);
        $black = imagecolorallocate($image,0,0,0);
        imagefill($image,0,0,$black);

//        $white = imagecolorallocate($image,255,255,255);
//        $text = 'Hello, GD!';
//        @imagettftext($image,20,0,70,100,$white,'',$text);

        imagepng($image,'test.png');

        imagedestroy($image);
    }

    public function actionT2() {
        $avatarUrl = '\em.png';
        $img = '\pq.jpg';
        $thumb = imagecreatetruecolor(206,206);
        $img_content = imagecreatefrompng(getcwd().$avatarUrl);//获取图片资源
        imagecopyresampled($thumb, $img_content, 0, 0, 0, 0, 206, 206, 320, 320);//核心函数，改变图片大小
        $file_name = "\qrcode_1.png";
        imagepng($thumb,getcwd().$file_name);//将图片资源保存到qrcode_1.png中
        $imgs = imagecreatefromjpeg(getcwd().$img);

//        print_r(imagesy($imgs));exit;

        imagecopy($imgs, $thumb, 237, 589, 0, 0, 206, 206);//核心函数：复制图片资源到另一图片资源中
        $f_file_name = "\last.jpg";
        imagejpeg($imgs,getcwd().$f_file_name); //保存
        imagedestroy($imgs);
        imagedestroy($thumb);//销毁图片资源
        //已生成二维码路径
//        2  $avatarUrl = '\qrcode.png';
// 3  //海报路径
// 4  $img = '\test.jpg';
// 5  //1、将微信二维码缩小至206*206，默认是430
// 6  $thumb = imagecreatetruecolor(206,206);     //创建一个新的画布（缩放后的），从左上角开始填充透明背景
// 7  $img_content = imagecreatefromjpeg(getcwd().$avatarUrl);//获取图片资源
// 8  //dump($img_content);
// 9  imagecopyresampled($thumb, $img_content, 0, 0, 0, 0, 206, 206, 430, 430);//核心函数，改变图片大小
//10  $file_name = "\qrcode_1.png";
//11  imagepng($thumb,getcwd().$file_name);//将图片资源保存到qrcode_1.png中
    }

    public function actionT23() {
//        echo getcwd();exit;
        $img1  = imagecreatefromjpeg('D:\wamp64\www\chengyun_unitqrcode\sites\api\pq.jpg');
        $img2 = imagecreatefrompng('D:\wamp64\www\chengyun_unitqrcode\sites\api\em.png');

//        $img=imagecopymerge($img1, $img2, 0, 0, 75, 75, 150, 150, 50);
        header('Content-Type: image/jpg');

//        imagejpeg($img,'acq3.jpg');

        imagedestroy($img1);
        imagedestroy($img2);


//        $width = 500;
//        $height = 300;
//        $html = '<html><body><h1>Hello World!</h1></body></html>';
//$image = imagecreatetruecolor($width, $height);
//$white = imagecolorallocate($image,255,255,255);
//$black = imagecolorallocate($image,0,0,0);
//imagefill($image,0,0,$white);
//$font = realpath( 'arial.ttf') ;
//$font = 'D:\wamp64\www\chengyun_unitqrcode\framework\yii\bower\swagger-ui\dist\fonts\DroidSans.ttf';
//imagettftext($image,20,0,10,30,$black,$font,$html);
//IM
//header('Content-Type: image/png');
//imagepng($image,'abc.png');
//imagedestroy($image);
    }

    public function actionT6() {
        $params = \Yii::$app->request->get();

        $result = GeoService::getInfoByCoord($params);

        print_r($result);exit;
    }

    public function actionT7() {
        $params = \Yii::$app->request->get();
        $result = UnitService::fillCood($params);
        print_r($result);exit;
    }

    public function actionT8() {
        $params = \Yii::$app->request->get();
        $r = GeoService::getAreaByCoord($params);
        print_r($r);exit;
    }

    public function actionT9() {
        set_time_limit(0);
        $count = 0;
//        $models = Unit::find()->where(['district_code'=>'01'])->all();


        $models = Unit::find()->where(['street_code'=>''])->orderBy(['id' => SORT_DESC])->all();
//echo count($models);
//exit;

        foreach ($models as $model) {
            $info = GeoService::getAreaByCoord(['lng'=>$model->lng,'lat'=>$model->lat]);
//            $model->district_code = $info['区代码'];
            $model->street_code = $info['街道代码'];
            $model->save();
            $count++;
        }

        echo $count;
        exit;
    }

    public function actionT10() {
        $models = Unit::find()->where(['has_bind'=>1,'images'=>''])->all();
        foreach ($models as $model) {
            $qb = QrcodeBind::find()->where(['unit_code'=>$model->unit_code,'is_delete'=>0])->asArray()->one();
            if (empty($qb)) {
                continue;
            }

            $model->images = $qb['images'];
            $model->save();
        }

        print_r(count($models));exit;
    }

    public function actionT11() {
        $username = \Yii::$app->request->get('username');
        $msg = '====LOGIN SUCCESS===='.$username.'======='.$this->getMillisecondTimeStamp();
        \Yii::info($msg,'data-api');

//      \Yii::getLogger()->log($msg,Logger::LEVEL_ERROR);

      exit;

    }

    function getMillisecondTimeStamp() {
        list($msec, $sec) = explode(' ', microtime());
        return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
    }

}
